package com.chuangjiangx.member.coupon.ddd.query;

import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.member.basic.ddd.dal.dto.StoreList;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrCouponDetail;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrCouponList;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrHasCouponDetail;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrHasCouponList;
import com.chuangjiangx.member.stored.ddd.dal.mapper.StoreInfoDalMapper;
import com.chuangjiangx.member.coupon.dao.mapper.InMbrCouponMapper;
import com.chuangjiangx.member.coupon.dao.mapper.InMbrHasCouponMapper;
import com.chuangjiangx.member.coupon.dao.model.InMbrCoupon;
import com.chuangjiangx.member.coupon.dao.model.InMbrCouponExample;
import com.chuangjiangx.member.coupon.dao.model.InMbrHasCoupon;
import com.chuangjiangx.member.coupon.dao.model.InMbrHasCouponExample;
import com.chuangjiangx.member.coupon.ddd.query.condition.MbrCouponListCondition;
import com.chuangjiangx.member.coupon.ddd.dal.mapper.MbrCouponDalMapper;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrHasCouponId;
import com.chuangjiangx.member.coupon.ddd.domain.model.VerifyStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 会员卡券查询
 */
@Component
public class MbrCouponQuery {

    private final MbrCouponDalMapper mbrCouponDalMapper;

    @Autowired
    private InMbrCouponMapper inMbrCouponMapper;
    @Autowired
    private InMbrHasCouponMapper inMbrHasCouponMapper;
    @Autowired
    private StoreInfoDalMapper storeInfoDalMapper;

    @Autowired
    public MbrCouponQuery(MbrCouponDalMapper mbrCouponDalMapper) {
        this.mbrCouponDalMapper = mbrCouponDalMapper;
    }

    /**
     * 卡券列表-商户
     */
    public PagingResult<MbrCouponList> findMbrCouponListForMerchant(MbrCouponListCondition condition) {
        return findMbrCouponListBase(condition);
    }

    /**
     * 卡券列表-店长
     */
    public PagingResult<MbrCouponList> findMbrCouponListForStore(MbrCouponListCondition condition) {
        Objects.requireNonNull(condition.getStoreId(),"门店id不能为空");
        List<Long> storeHasCouponList = findStoreHasCouponList(condition.getStoreId());
        condition.setMbrCouponIds(storeHasCouponList);
        return findMbrCouponListBase(condition);
    }

    private PagingResult<MbrCouponList> findMbrCouponListBase(MbrCouponListCondition condition) {
        PagingResult<MbrCouponList> result = new PagingResult<>(condition.getPageNumber(),condition.getPageSize());
        Integer total = mbrCouponDalMapper.countMbrCouponList(condition);
        result.setTotal(total);
        if (total > 0) {
            List<MbrCouponList> mbrCouponLists = mbrCouponDalMapper.findMbrCouponList(condition);
            result.setItems(mbrCouponLists);
        }
        return result;
    }

    /**
     * 加载商户下门店列表
     */
    public List<StoreList> findStoreListByMerchantId(Long merchantId, String name) {
        if (merchantId == null) {
            throw new IllegalArgumentException("商户id不能为空");
        }
        return mbrCouponDalMapper.findStoreListByMerchantId(merchantId,name);
    }

    /**
     * 加载卡券可使用门店列表
     */
    public List<StoreList> findStoreListByCouponNumber(String couponNumber, String name) {
        if (couponNumber == null) {
            throw new IllegalArgumentException("卡券id不能为空");
        }
        return mbrCouponDalMapper.findStoreListByCouponNumber(couponNumber, name);
    }


    /**
     * 加载门店下有的卡券
     */
    public List<Long> findStoreHasCouponList(Long storeId) {
        Objects.requireNonNull(storeId);
        return mbrCouponDalMapper.findStoreHasCouponList(storeId);
    }

    /**
     * 加载会员领取的卡券-H5
     * @param memberId 会员id
     * @param status 使用状态 0-未使用 1-已使用 2-已过期 3-已冻结
     */
    public List<MbrHasCouponList> findMbrHasCouponList(Long memberId, Integer status) {
        return mbrCouponDalMapper.findMbrHasCouponList(memberId, status);
    }

    /**
     * 获取卡券详情
     * @param couponNumber 卡券id
     */
    public MbrCouponDetail getMbrCouponDetailInfo(String couponNumber) {
        Objects.requireNonNull(couponNumber);
        MbrCouponDetail mbrCouponDetail = mbrCouponDalMapper.getMbrCouponDetailInfo(couponNumber);
        if (mbrCouponDetail == null) {
            throw new BaseException("","卡券信息不存在");
        }
        //门店数量
        List<StoreList> stores = mbrCouponDalMapper.findStoresByMbrHasCouponId(mbrCouponDetail.getId());
        mbrCouponDetail.setTotalStoreNum(stores.size());
        mbrCouponDetail.setStoreLists(stores);
        if(!stores.isEmpty()){
            if(stores.size() == 1){
                mbrCouponDetail.setOnlyOneStoreName(stores.get(0).getName());
            }
            String collect = stores
                    .stream()
                    .map(s -> String.valueOf(s.getId()))
                    .collect(Collectors.joining(","));
            mbrCouponDetail.setStoreIds(collect);

        }

        return mbrCouponDetail;
    }

    /**
     * 获取会用拥有卡券详情
     * @param mbrHasCouponId 会员拥有卡券id
     * @return 详情
     */
    public MbrHasCouponDetail getMbrHasCouponDetail(MbrHasCouponId mbrHasCouponId) {
        MbrHasCouponDetail detail = mbrCouponDalMapper.getMbrHasCouponDetail(mbrHasCouponId.getId());
        if (detail == null) {
            throw new BaseException("","卡券信息不存在");
        }
        List<StoreList> storeList = mbrCouponDalMapper.findStoreListByCouponNumber(detail.getCouponNumber(),null);
        detail.setStoreLists(storeList);
        return detail;
    }

    /**
     * 查询会员最新领取的卡券
     * @param memberId
     * @param couponNumber
     */
    public InMbrHasCoupon getNewestReceived(Long memberId, String couponNumber){
        InMbrCouponExample e = new InMbrCouponExample();
        e.createCriteria().andCouponNumberEqualTo(couponNumber);
        List<InMbrCoupon> inMbrCoupons = inMbrCouponMapper.selectByExample(e);
        if(inMbrCoupons.isEmpty()){
            return null;
        }
        Long mbrCouponId = inMbrCoupons.get(0).getId();
        InMbrHasCouponExample example = new InMbrHasCouponExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId)
                .andMbrCouponIdEqualTo(mbrCouponId)
                .andVerifyStatusEqualTo(VerifyStatus.UNUSED.code);
        example.setOrderByClause("create_time desc");
        List<InMbrHasCoupon> inMbrHasCoupons = inMbrHasCouponMapper.selectByExample(example);
        return inMbrHasCoupons.isEmpty() ? null : inMbrHasCoupons.get(0);
    }



    /**
     * 检查是否是会员的卡券
     */
    public boolean isMbrCoupon(String wxCardId){
        InMbrCouponExample example = new InMbrCouponExample();
        example.createCriteria().andWxCardIdEqualTo(wxCardId);
        long count = inMbrCouponMapper.countByExample(example);
        return count>0;
    }

    public InMbrHasCoupon queryInMbrHasCoupon(String couponVerifyCode){
        InMbrHasCouponExample example = new InMbrHasCouponExample();
        example.createCriteria()
                .andVerifyCodeEqualTo(couponVerifyCode);
        List<InMbrHasCoupon> inMbrHasCoupons = inMbrHasCouponMapper.selectByExample(example);
        if(inMbrHasCoupons.isEmpty()){
            return null;
        }
        if(inMbrHasCoupons.size() > 1){
            throw new BaseException("",couponVerifyCode + "查询到" +inMbrHasCoupons.size()+ "条数据!");
        }
        return inMbrHasCoupons.get(0);
    }
}
